unit scSmsFilter;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, sGroupBox, StdCtrls, Buttons, sBitBtn, sComboBox, Mask,
  sMaskEdit, sCustomComboEdit, sTooledit, sEdit, scFilterParam, DateUtils,
  sCheckBox, DBCtrls, sDBLookupComboBox, DB, ExtCtrls, sBevel, ZDataset,
  sSpinEdit;

type
  TfmSmsFilter = class(TForm)
    grpPeriod: TsGroupBox;
    sbtbtnOk: TsBitBtn;
    sbtbtnCacel: TsBitBtn;
    edtD1: TsDateEdit;
    edtD2: TsDateEdit;
    cbbDateFilter: TsComboBox;
    grpState: TsGroupBox;
    chkNew: TsCheckBox;
    chkWork: TsCheckBox;
    chkReady: TsCheckBox;
    chkNotIn: TsCheckBox;
    chkOutDate: TsCheckBox;
    cbbResp: TsDBLookupComboBox;
    cbbReview: TsDBLookupComboBox;
    cbbCode: TsDBLookupComboBox;
    dsCode: TDataSource;
    dsRview: TDataSource;
    dsResp: TDataSource;
    sbvl1: TsBevel;
    edtPhone: TsEdit;
    edtResp: TsEdit;
    cbbResolution: TsDBLookupComboBox;
    dsResol: TDataSource;
    edtShift: TsDecimalSpinEdit;
    procedure cbbDateFilterCloseUp(Sender: TObject);
    procedure cbbDateFilterChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cbbRespKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure edtRespChange(Sender: TObject);
    procedure cbbRespCloseUp(Sender: TObject);
  private
    procedure setPeriod;
  public
    procedure setControlsByParam(param : TFilterParam);
    procedure setParamByControls(param : TFilterParam);
  end;

implementation

uses scDmDao;

{$R *.dfm}  

procedure TfmSmsFilter.setPeriod;
begin
  case cbbDateFilter.ItemIndex of
    0 : begin
          edtD1.Date := Date;
          edtD2.Date := Date;
        end;
    1 : begin
          edtD1.Date := StartOfTheWeek(Date);
          edtD2.Date := EndOfTheWeek(Date);
        end;
    2 : begin
          edtD1.Date := StartOfTheMonth(Date);
          edtD2.Date := EndOfTheMonth(Date);
        end;
    3 : begin
          edtD1.Date := StartOfTheYear(Date);
          edtD2.Date := EndOfTheYear(Date);
        end;
  else
  end;

end;

procedure TfmSmsFilter.cbbDateFilterCloseUp(Sender: TObject);
begin
  setPeriod;
end;

procedure TfmSmsFilter.setControlsByParam(param: TFilterParam);
begin
  edtD1.Date := param.d1;
  edtD2.Date := param.d2;
  cbbDateFilter.ItemIndex := param.cbbIndex;
  chkNew.Checked := 1 in param.orderState;
  chkWork.Checked := 2 in param.orderState;
  chkReady.Checked := 3 in param.orderState;
  chkNotIn.Checked := 4 in param.orderState;
  chkOutDate.Checked := 5 in param.orderState;
  cbbResolution.KeyValue := param.resolution;
  edtPhone.Text := param.phone;
  cbbCode.KeyValue := param.code;
  cbbResp.KeyValue := param.resp;
  cbbReview.KeyValue := param.review;
  edtShift.Value := param.shift_count;
end;

procedure TfmSmsFilter.setParamByControls(param: TFilterParam);
begin
   param.d1 := edtD1.Date;
   param.d2 := edtD2.Date;
   param.cbbIndex := cbbDateFilter.ItemIndex;
   param.orderState := [];
   if chkNew.Checked then param.orderState := param.orderState + [1];
   if chkWork.Checked then param.orderState := param.orderState + [2];
   if chkReady.Checked then param.orderState := param.orderState + [3];
   if chkNotIn.Checked then param.orderState := param.orderState + [4];
   if chkOutDate.Checked then param.orderState := param.orderState + [5];
   param.resolution := cbbResolution.KeyValue;
   param.resolution_str := cbbResolution.Text;
   param.phone := edtPhone.Text;
   param.code := cbbCode.KeyValue;
   param.code_str := cbbCode.Text;
   param.resp := cbbResp.KeyValue;
   param.resp_str := cbbResp.Text;
   param.review := cbbReview.KeyValue;
   param.review_str := cbbReview.Text;
   param.shift_count := Trunc(edtShift.Value);
end;

procedure TfmSmsFilter.cbbDateFilterChange(Sender: TObject);
begin
  edtD1.ReadOnly := not (cbbDateFilter.ItemIndex = 4);
  edtD2.ReadOnly := edtD1.ReadOnly;
end;

procedure TfmSmsFilter.FormCreate(Sender: TObject);
begin
  dsCode.DataSet := dmDao.getCode(Self);
  dsResp.DataSet := dmDao.getResponsibleFio(Self);
  dmDao.setSerchResp(TZQuery(dsResp.DataSet));
  dsRview.DataSet := dmDao.getReview(Self);
  dsResol.DataSet := dmDao.getResolution(Self);
end;

procedure TfmSmsFilter.cbbRespKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_DELETE then
   TsDBLookupComboBox(Sender).KeyValue := 0;
end;

procedure TfmSmsFilter.edtRespChange(Sender: TObject);
begin
  with Sender as TEdit do
  begin
    if Text = '' then Exit;
    dmDao.setSerchResp(TZQuery(dsResp.DataSet), Text);
    cbbResp.DropDown;
  end;
end;

procedure TfmSmsFilter.cbbRespCloseUp(Sender: TObject);
begin
  edtResp.Text := '';
end;

end.
